using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Timers;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Support.V4.Widget;
using Android.Support.V7.App;
using Android.Support.V7.Widget;
using Android.Text;
using Android.Text.Style;
using Android.Util;
using Android.Views;
using Android.Widget;
using Com.Gcssloop.Widget;
using mb;
using mb.Droid;

namespace libmp
{
    public class DemandFrequencyView
    {
        static ILog logger = ServiceLocator.Instance.Get<ILog>();

        public static MyDialogFragment CreateFragment()
        {
            return new MyDialogFragment()
            {
                MyOnCreateView = (f, container) => CreateView(f),
                    MyOnResume = (f) =>
                    {
                        f.Dialog.Window.SetLayout(-2, -2);
                    }
            };
        }

        public static View CreateView(MyDialogFragment f)
        {
            f.Cancelable = true; //设置点击空白区域不返回
            f.Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
            var ctx = StartActivity.This;

            Point outSize = new Point();
            ctx.WindowManager.DefaultDisplay.GetRealSize(outSize);

            LinearLayout FLayout = new LinearLayout(ctx);

            RCRelativeLayout demandRCLayout = new RCRelativeLayout(ctx);
            demandRCLayout.SetVerticalGravity(GravityFlags.CenterVertical);
            demandRCLayout.SetRadius(20);

            LinearLayout demandLayout = new LinearLayout(FLayout.Context);
            demandLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
            demandLayout.Orientation = Orientation.Vertical;
            demandLayout.SetBackgroundColor(Color.White);
            demandRCLayout.AddView(demandLayout);

            LinearLayout demandLayoutTop = new LinearLayout(FLayout.Context);
            demandLayoutTop.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 340);
            demandLayoutTop.Orientation = Orientation.Horizontal;
            DemandLayoutTop(demandLayoutTop, f);
            demandLayout.AddView(demandLayoutTop);

            LinearLayout demandLayoutBottom = new LinearLayout(FLayout.Context);
            demandLayoutBottom.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 550);
            demandLayoutBottom.SetBackgroundColor(Color.Rgb(251, 114, 153));
            demandLayoutBottom.Orientation = Orientation.Vertical;
            DemandLayoutBottom(demandLayoutBottom);
            demandLayout.AddView(demandLayoutBottom);
            FLayout.AddView(demandRCLayout, new LinearLayout.LayoutParams(outSize.X - 150, 890));

            return FLayout;

        }

        static void DemandLayoutTop(LinearLayout demandLayoutTop, MyDialogFragment f)
        {

            LinearLayout demandLayoutLeft = new LinearLayout(demandLayoutTop.Context);
            demandLayoutLeft.LayoutParameters = new LinearLayout.LayoutParams(100, LinearLayout.LayoutParams.MatchParent);
            var returnImageView = new ImageView(demandLayoutTop.Context);
            returnImageView.SetImageDrawable(Util.DrawableFromFile("popup/btn_back.png"));
            returnImageView.LayoutParameters = new LinearLayout.LayoutParams(40, 40)
            {
                LeftMargin = 20,
                TopMargin = 20
            };
            demandLayoutLeft.AddView(returnImageView);
            demandLayoutTop.AddView(demandLayoutLeft);
            demandLayoutLeft.Click += (s, e) =>
            {
                logger.Log("返回-----------");
                f.Dismiss();
            };

            LinearLayout demandLayoutCenter = new LinearLayout(demandLayoutTop.Context);
            demandLayoutCenter.LayoutParameters = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MatchParent, 1);
            demandLayoutCenter.Orientation = Orientation.Vertical;
            demandLayoutCenter.SetHorizontalGravity(GravityFlags.CenterHorizontal);
            var expressionImageView = new ImageView(demandLayoutTop.Context);
            if (User.me.VideoCanPlayRemain > 0)
            {
                expressionImageView.SetImageDrawable(Util.DrawableFromFile("popup/em_good.png"));
            }
            else
            {
                expressionImageView.SetImageDrawable(Util.DrawableFromFile("popup/em_bad.png"));
            }

            expressionImageView.LayoutParameters = new LinearLayout.LayoutParams(150, 150) { TopMargin = 80 };
            LinearLayout textViewLayoutCenter = new LinearLayout(demandLayoutTop.Context);
            textViewLayoutCenter.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 80) { TopMargin = 10 };
            textViewLayoutCenter.SetGravity(GravityFlags.Center);
            var textView = new TextView(demandLayoutTop.Context);
            if (User.me.VideoCanPlayRemain > 0)
            {
                textView.Text = "您当日的点播次数还剩" + User.me.VideoCanPlayRemain + "次!";
            }
            else
            {
                textView.Text = "您当日的点播次数已经用完!";
            }

            textView.SetTextColor(Color.Rgb(251, 114, 153));
            textView.SetTextSize(ComplexUnitType.Pt, 7);
            textView.SetTypeface(Typeface.Default, TypefaceStyle.Bold);
            textViewLayoutCenter.AddView(textView);
            demandLayoutCenter.AddView(expressionImageView);
            demandLayoutCenter.AddView(textViewLayoutCenter);
            demandLayoutTop.AddView(demandLayoutCenter);

            LinearLayout demandLayoutRight = new LinearLayout(demandLayoutTop.Context);
            demandLayoutRight.LayoutParameters = new LinearLayout.LayoutParams(100, LinearLayout.LayoutParams.MatchParent);
            var closeImageView = new ImageView(demandLayoutTop.Context);
            closeImageView.SetImageDrawable(Util.DrawableFromFile("popup/btn_close.png"));
            closeImageView.LayoutParameters = new LinearLayout.LayoutParams(40, 40)
            {
                LeftMargin = 40,
                TopMargin = 20
            };
            demandLayoutRight.AddView(closeImageView);

            demandLayoutRight.Click += (s, e) =>
            {
                logger.Log("关闭------------");
                f.Dismiss();
            };
            demandLayoutTop.AddView(demandLayoutRight);
        }

        static void DemandLayoutBottom(LinearLayout demandLayoutBottom)
        {
            LinearLayout invitationLayout = new LinearLayout(demandLayoutBottom.Context);
            invitationLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 260);
            invitationLayout.Orientation = Orientation.Vertical;
            var friendLayout = new LinearLayout(demandLayoutBottom.Context);
            friendLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 60) { LeftMargin = 40, TopMargin = 20 };
            friendLayout.SetVerticalGravity(GravityFlags.CenterVertical);
            var friendTextView = new TextView(demandLayoutBottom.Context);

            friendTextView.Text = "邀请好友观看,增加永久观看次数";
            friendTextView.SetTextColor(Color.White);
            friendTextView.SetTextSize(ComplexUnitType.Pt, 6);
            friendTextView.SetTypeface(Typeface.Default, TypefaceStyle.Bold);
            friendLayout.AddView(friendTextView);
            invitationLayout.AddView(friendLayout);

            var friendResultLayout = new LinearLayout(demandLayoutBottom.Context);
            friendResultLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 60) { LeftMargin = 40, TopMargin = 20 };
            friendResultLayout.SetVerticalGravity(GravityFlags.CenterVertical);
            var friendResultLayoutTextView = new TextView(demandLayoutBottom.Context);

            friendResultLayoutTextView.Text = "每邀请一个好友安装 = 永久增加每日播放次数1次";
            friendResultLayoutTextView.SetTextColor(Color.White);
            friendResultLayoutTextView.SetTextSize(ComplexUnitType.Pt, 6);
            friendResultLayout.AddView(friendResultLayoutTextView);
            invitationLayout.AddView(friendResultLayout);

            var friendButtonLayout = new LinearLayout(demandLayoutBottom.Context);
            friendButtonLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 80) { TopMargin = 20 };
            friendButtonLayout.SetGravity(GravityFlags.Center);

            //圆角
            RCRelativeLayout friendButtonRCLayout = new RCRelativeLayout(demandLayoutBottom.Context);
            friendButtonRCLayout.SetVerticalGravity(GravityFlags.CenterVertical);
            var friendButtonRCLayoutParams = new LinearLayout.LayoutParams(400, 80);
            friendButtonRCLayout.LayoutParameters = friendButtonRCLayoutParams;
            friendButtonRCLayout.SetRadius(15);

            LinearLayout btnfriendButton = new LinearLayout(demandLayoutBottom.Context);
            var btnfriendButtonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
            btnfriendButton.LayoutParameters = btnfriendButtonParams;
            btnfriendButton.SetBackgroundColor(Color.White);
            btnfriendButton.SetGravity(GravityFlags.Center);
            friendButtonRCLayout.AddView(btnfriendButton);

            var friendButtonTextView = new TextView(demandLayoutBottom.Context);
            friendButtonTextView.Text = "邀请好友观看";
            friendButtonTextView.SetTextColor(Color.Rgb(251, 114, 153));
            friendButtonTextView.SetTypeface(Typeface.Default, TypefaceStyle.Bold);
            friendButtonTextView.SetTextSize(ComplexUnitType.Pt, 6);
            btnfriendButton.AddView(friendButtonTextView);
            btnfriendButton.Click += (s, e) =>
            {
                logger.Log("邀请好友观看--------");
                // User.me.VideoCanPlayRemain++;
                // UpdateViewNums(User.me.id, User.me.VideoCanPlayRemain, true);
                // User.me.Save();
            };
            friendButtonLayout.AddView(friendButtonRCLayout);
            invitationLayout.AddView(friendButtonLayout);
            demandLayoutBottom.AddView(invitationLayout);

            LinearLayout vipLayout = new LinearLayout(demandLayoutBottom.Context);
            vipLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 260);
            vipLayout.Orientation = Orientation.Vertical;
            var purchaseVipLayout = new LinearLayout(demandLayoutBottom.Context);
            purchaseVipLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 60) { LeftMargin = 40, TopMargin = 20 };
            purchaseVipLayout.SetVerticalGravity(GravityFlags.CenterVertical);
            // friendLayout.SetBackgroundColor(Color.Red);
            var purchaseVipTextView = new TextView(demandLayoutBottom.Context);

            purchaseVipTextView.Text = "购买VIP会员";
            purchaseVipTextView.SetTextColor(Color.White);
            purchaseVipTextView.SetTextSize(ComplexUnitType.Pt, 6);
            purchaseVipTextView.SetTypeface(Typeface.Default, TypefaceStyle.Bold);
            purchaseVipLayout.AddView(purchaseVipTextView);
            // invitationLayout.SetBackgroundColor(Color.Red);
            vipLayout.AddView(purchaseVipLayout);

            var purchaseVipResultLayout = new LinearLayout(demandLayoutBottom.Context);
            purchaseVipResultLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 60) { LeftMargin = 40, TopMargin = 20 };
            purchaseVipResultLayout.SetVerticalGravity(GravityFlags.CenterVertical);
            // friendResultLayout.SetBackgroundColor(Color.White);
            var purchaseVipResultLayoutTextView = new TextView(demandLayoutBottom.Context);

            purchaseVipResultLayoutTextView.Text = "4大至尊特权,享受极速播放体验";
            purchaseVipResultLayoutTextView.SetTextColor(Color.White);
            purchaseVipResultLayoutTextView.SetTextSize(ComplexUnitType.Pt, 6);
            purchaseVipResultLayout.AddView(purchaseVipResultLayoutTextView);
            vipLayout.AddView(purchaseVipResultLayout);

            var purchaseVipButtonLayout = new LinearLayout(demandLayoutBottom.Context);
            purchaseVipButtonLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 80) { TopMargin = 20 };
            // friendButtonLayout.SetVerticalGravity(GravityFlags.CenterVertical);
            purchaseVipButtonLayout.SetGravity(GravityFlags.Center);

            //圆角
            RCRelativeLayout purchaseVipButtonRCLayout = new RCRelativeLayout(demandLayoutBottom.Context);
            purchaseVipButtonRCLayout.SetVerticalGravity(GravityFlags.CenterVertical);
            var purchaseVipButtonRCLayoutParams = new LinearLayout.LayoutParams(400, 80);
            // registerRCLayoutParams.LeftMargin = 40;
            purchaseVipButtonRCLayout.LayoutParameters = purchaseVipButtonRCLayoutParams;
            purchaseVipButtonRCLayout.SetRadius(15);

            LinearLayout purchaseVipButton = new LinearLayout(demandLayoutBottom.Context);
            purchaseVipButton.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
            purchaseVipButton.SetBackgroundColor(Color.White);
            purchaseVipButton.SetGravity(GravityFlags.Center);
            purchaseVipButtonRCLayout.AddView(purchaseVipButton);

            var purchaseVipButtonTextView = new TextView(demandLayoutBottom.Context);
            purchaseVipButtonTextView.Text = "购买VIP会员";
            purchaseVipButtonTextView.SetTextColor(Color.Rgb(251, 114, 153));
            purchaseVipButtonTextView.SetTypeface(Typeface.Default, TypefaceStyle.Bold);
            purchaseVipButtonTextView.SetTextSize(ComplexUnitType.Pt, 6);
            purchaseVipButton.AddView(purchaseVipButtonTextView);
            purchaseVipButton.Click += (s, e) =>
            {
                logger.Log("购买VIP会员--------");
            };
            purchaseVipButtonLayout.AddView(purchaseVipButtonRCLayout);
            vipLayout.AddView(purchaseVipButtonLayout);
            demandLayoutBottom.AddView(vipLayout);
        }

        public static void UpdateViewNums(string id, int viewsNums, bool changeMax)
        {
            var baseUrl = App.CurrentServerUrl;
            var body = LitJson.JsonMapper.ToJson(new { id = id, viewsNums = viewsNums, changeMax = changeMax });
            var url = $"http://{baseUrl}/user/updateViewNums";
            var res = Http.MakeRequest(new Http.Option
            {
                Url = url,
                    Method = HttpMethod.Post,
                    Body = body,
                    OnFinish = (ok, code, msg) =>
                    {
                        logger.Log("msg:" + msg);
                        if (code == 500)
                        {
                            logger.Log("----更新错误");
                        }
                        else
                        {
                            logger.Log("----更新成功");
                        }

                    },
                    OnProgress = (total, now) =>
                    {
                        logger.Log("progress", total, now);
                    }
            });
        }
    }
}